package com.tongji.showroom.view
{ 
	import com.tongji.showroom.events.GetUserSceneEvent;
	import com.tongji.showroom.events.NavigatorEvent;
	import com.tongji.showroom.model.PictureModel;
	import com.tongji.showroom.model.SearchSceneModel;
	import com.tongji.showroom.model.UserModel;
	import com.tongji.showroom.model.UserSceneModel;
	import com.tongji.showroom.utils.PageTools;
	import com.tongji.showroom.view.twoD.AddBitmap;
	import com.tongji.showroom.view.twoD.Furniture;
	import com.tongji.showroom.view.twoD.OldDistortSprite;
	import com.tongji.showroom.vo.ObjectTranslator;
	import com.tongji.showroom.vo.SavePicture;
	import com.tongji.showroom.vo.SceneInfo;
	import com.tongji.showroom.vo.UserAngle;
	
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.graphics.codec.JPEGEncoder;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author jiarong
	 * 
	 */
	public class ReleaseScenceMediator extends Mediator
	{
		[Inject]
		public var releaseView:ReleaseSenceComp;
		
		[Inject]
		public var userScene:UserSceneModel;
		
		[Inject]
		public var pictureModel:PictureModel;
		
		[Inject]
		public var model:SearchSceneModel;
		
		[Inject]
		public var userModel:UserModel;
		
		/*private var angle45:Object;
		private var angle135:Object;
		private var angle90:Object;*/
		
		public static var oriWidth:Number = 1024; //图片的原始高宽
		public static var oriHeight:Number = 768;
		public static var rectHeight:Number = 450; //默认高宽的限制
		public static var rectWidth:Number = 600;
		[Bindable]
		public var sceneInfoList:ArrayCollection;
		private var sceneAngle:Object = new Object();
		private var backGroundBitmap:AddBitmap;
		private var angle45:Object;
		private var angle135:Object;
		private var angle90:Object;
		
		private var pageIndex:int = 1;
		public function ReleaseScenceMediator()
		{
			super();
		}
		
		override public function onRegister():void
		{
			releaseView.shareRadioBtn.addEventListener(MouseEvent.CLICK, radiobutton1_clickHandler);
			BindingUtils.bindProperty(releaseView.saveSceneList, "dataProvider", userScene, "getUserSceneArr");	
			eventMap.mapListener(releaseView.saveSceneList, IndexChangeEvent.CHANGE, showSceneHandler, IndexChangeEvent);
			eventMap.mapListener(releaseView.viewcomp.angle135Btn, MouseEvent.CLICK, change);
			eventMap.mapListener(releaseView.viewcomp.angle45Btn, MouseEvent.CLICK, change);
			eventMap.mapListener(releaseView.viewcomp.angle90Btn, MouseEvent.CLICK, change);
			addContextListener(GetUserSceneEvent.UPDATE_USER_SCENE, setPagesHandler, GetUserSceneEvent);
			addViewListener(NavigatorEvent.PAGE_INDEX_CHANGE, changePageHandler, NavigatorEvent);			

			var page:PageTools = new PageTools(userScene.recordCount, 5, 1, pageIndex);
			releaseView.pageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		
		private function changePageHandler(event:NavigatorEvent):void
		{
			pageIndex = event.pageIndex;
			dispatch(new GetUserSceneEvent(GetUserSceneEvent.GET_USER_SCENE, userModel.userID, pageIndex, 1));
		}
		
		private function setPagesHandler(event:GetUserSceneEvent):void
		{
			var page:PageTools = new PageTools(userScene.recordCount, 5, 1, pageIndex);
			releaseView.pageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		
		private function showSceneHandler(event:IndexChangeEvent):void
		{			
			//按场景角度设置场景角度变换按钮的可用性
			userScene.currentUserScene = releaseView.saveSceneList.selectedItem;
			for each (var userAngle:Object in releaseView.saveSceneList.selectedItem.SceneAngleList)
			{
				switch (userAngle.Jdid)
				{
					case 30://正45°
						releaseView.viewcomp.angle45Btn.enabled = true;
						releaseView.workplace.source = userAngle.Bgurl;
						angle45 = userAngle;
						break;
					case 31://负45°
						releaseView.viewcomp.angle135Btn.enabled = true;
						releaseView.workplace.source = userAngle.Bgurl;
						angle135 = userAngle;
						break;
					case 35://90°
						releaseView.viewcomp.angle90Btn.enabled = true;
						releaseView.workplace.source = userAngle.Bgurl;
						angle90 = userAngle;
						break;
					default:
						break;						
				}
			}			
		}

		private function change(event:MouseEvent):void
		{
			if (releaseView.viewcomp.angle45Btn.selected == true)
			{
				releaseView.workplace.source = angle45.Bgurl;				
			}				
			if (releaseView.viewcomp.angle90Btn.selected == true)
			{
				releaseView.workplace.source = angle90.Bgurl;				
			}
			if (releaseView.viewcomp.angle135Btn.selected == true)
			{
				releaseView.workplace.source = angle135.Bgurl;				
			}			
		}
		
		/*private function downLoadClickHandler(event:MouseEvent):void
		{
			var bitmapData:BitmapData = new BitmapData(releaseView.showStage.width, releaseView.showStage.height);
			bitmapData.draw(releaseView.showStage);
			var jpg:JPEGEncoder = new JPEGEncoder();
			var jpgByteArray:ByteArray = jpg.encode(bitmapData);
			var fr:FileReference = new FileReference;
			fr.save(jpgByteArray, "导出.jpg");
		}*/

		
		private function changeAngle(event:MouseEvent):void //chenzhongyuan
		{
			sceneInfoList = new ArrayCollection();
			
			if (releaseView.viewcomp.angle45Btn.selected == true)
			{
				sceneAngle = angle45;
			}				
			if (releaseView.viewcomp.angle90Btn.selected == true)
			{
				sceneAngle = angle90;
			}
			if (releaseView.viewcomp.angle135Btn.selected == true)
			{
				sceneAngle = angle135;
			}
			
			//releaseView.workplace.image.source = sceneAngle.Bgurl;
			var backGroundBitmap:AddBitmap = new AddBitmap(sceneAngle.Bgurl, oriWidth / 2, oriHeight / 2);
			backGroundBitmap.scaleX = 500 / 600;
			backGroundBitmap.scaleY = 360/ 450;
			releaseView.workplace.addChild(backGroundBitmap);
			
			var maxDepth:int = 0;
			for each(var sceneInfo1:Object in sceneAngle.SceneInfoList)
			{
				if (sceneInfo1.depth > maxDepth)
					maxDepth = sceneInfo1.depth;
			}
			
			var i:int = maxDepth;
			
			while (i > 0)
			{
				for each(var sceneInfo:Object in sceneAngle.SceneInfoList)
				{				
					if (sceneInfo.depth == i)
					{
						var addBitmap:AddBitmap = new AddBitmap(sceneInfo.ProductUrl, sceneInfo.xAxis, sceneInfo.yAxis);
						addBitmap.scaleX = 500 / 600;
						addBitmap.scaleY = 360/ 450;	
						releaseView.workplace.addChild(addBitmap);
						sceneInfo.sceneInfoBitmap = addBitmap;
						if(sceneInfo.sceneInfoVisible == null)
							sceneInfo.sceneInfoVisible = true;
						addBitmap.visible = sceneInfo.sceneInfoVisible;
						ObjectTranslator.objectToInstance(sceneInfo, SceneInfo);
						sceneInfoList.addItem(sceneInfo);
						i--;
					}	
				}	
			}
		}
		
		private function radiobutton1_clickHandler(event:MouseEvent):void
		{
			if (releaseView.shareRadioBtn.selected)
			{
				releaseView.currentState = 'showShare';
				
				releaseView.renrenBtn.addEventListener(MouseEvent.CLICK, clickHandle);
				releaseView.sinaBtn.addEventListener(MouseEvent.CLICK, clickHandle);
				releaseView.kaixinBtn.addEventListener(MouseEvent.CLICK, clickHandle);
				releaseView.tenxunBtn.addEventListener(MouseEvent.CLICK, clickHandle);
				releaseView.baishehuiBtn.addEventListener(MouseEvent.CLICK, clickHandle);
				releaseView.doubanBtn.addEventListener(MouseEvent.CLICK,clickHandle);
				
			}
			else
			{
				releaseView.currentState = 'hideShare';
			}
		}
		private function clickHandle(event:MouseEvent):void
		{
			var currentURL:String = "http://space.rryjw.com/Member/Photo/Details.aspx?UserID=43716&PhotoID=2826";
			var title:String = "test";
			var image:String = '';
			
			switch(event.target.id)
			{
				case "renrenBtn":
					var renrenURL:String = " http://share.renren.com/share/buttonshare.do?link=" + currentURL + "&image";
					navigateToURL(new URLRequest(renrenURL),"_blank");
					break;
			    case "sinaBtn":
					var sinaURL:String = "http://v.t.sina.com.cn/share/share.php?url=" + currentURL + "&title=" + title + "&image";
					navigateToURL(new URLRequest(sinaURL),"_blank");
					break;
				case "tenxunBtn":
					var tenxunURL:String = "http://v.t.qq.com/share/share.php?title=" + title + "&url=" + currentURL + "&pic";
					navigateToURL(new URLRequest(tenxunURL),"_blank");
					break;
				case "kaixinBtn":
					var kaixinURL:String = "http://www.kaixin001.com/repaste/share.php??title=" + title  + "&rurl=" + currentURL + "&image";
					navigateToURL(new URLRequest(kaixinURL),"_blank");
					break;
				case "baishehuiBtn":
					var baishehuiURL:String = "http://bai.sohu.com/share/blank/add.do?link=" + currentURL;
					navigateToURL(new URLRequest(baishehuiURL),"_blank");
					break;
				case "doubanBtn":
					var doubanURL:String = "http://www.douban.com/recommend/?url=" + currentURL + "&title=" + title;
					navigateToURL(new URLRequest(doubanURL),"_blank");
					break;
			}
		}
	}
}